home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14924 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.8 KB  |  103 lines

  1. Path: in1.uu.net!novia!usenet
  2. From: srwillrd@novia.net (William E. Kempf)
  3. Newsgroups: comp.lang.c++,comp.os.ms-windows.programmer.misc
  4. Subject: Re: Virtual function question
  5. Date: Tue, 02 Apr 1996 19:27:02 GMT
  6. Organization: Novia Internetworking <> 28.8kbps dialup; 402/390-2NET
  7. Message-ID: <316178f3.1131475816@204.248.25.97>
  8. References: <4jp6e9$ou5@dub-news-svc-1.compuserve.com>
  9. NNTP-Posting-Host: 167.16.65.84
  10. X-Newsreader: Forte Agent .99e/32.194
  11.  
  12. RossBoylan@aol.com (Ross Boylan) wrote:
  13.  
  14. :I am trying to define, a la Smalltalk, a mix-in which defines the
  15. :various comparison operators in terms of one fundamental operator, <.
  16. :
  17. :
  18. :typedef int bool;
  19. :class RBMagnitude {
  20. :public:
  21. ://subclass should redefine < with appropriate types
  22. :    virtual bool operator<(const RBMagnitude&) const= 0;
  23. :    virtual bool operator>(const RBMagnitude& x) const {return  *this <
  24. :x;};
  25. :// and many more
  26. :};
  27. :
  28. :class A : public RBMagnitude {
  29. :public:
  30. :    virtual bool operator<(const A& a) const {return (this->n < a.n);};
  31. :private:
  32. :    int n;
  33. :};
  34. :
  35. :
  36. :class A test;
  37. :
  38. :This fails in MSVC++ 4.0 with the error that A is virtual because the
  39. :operator<(const RBMagnitude&) is not defined.
  40. :It looks to me as if this is because the operator< for A has a
  41. :different type signature (it uses an A rather than an RBMagnitude).
  42. :If so, the problem is the language definition, not Microsoft's
  43. :implementation.
  44. :
  45. :1) Is this interpretation correct?
  46. :2) Is there any way around this problem?  Commenting out the
  47. :definition of RBMagnitude::operator< doesn't work, because
  48. :RBMagnitude::operator> requires it.  I suppose I could define a bogus
  49. :operator< which throws an exception, but this seems awkward (in
  50. :particular, if A and B are both RBMagnitudes and I ask A<B, I fail at
  51. :run time rather than compile time).
  52. :
  53. :I would appreciate e-mail; I'll summarize here.
  54. :
  55.  
  56. Well, I have a few corrections.  This can be done in C++, in fact
  57. Borland used to have this defined for their base object class in their
  58. class libraries.
  59.  
  60. Firstly, there is no way that I know of to define all the "equality"
  61. operators off of just one (it requires two of them).  Secondly, your >
  62. operator is returning the value for <.  Serious bug.  Thirdly, do
  63. *NOT* typedef a bool type!  This will soon be a keyword in the C++
  64. language, and will likely break your code.
  65.  
  66. Finally, yes the parameters have to be the same type.  You'll have to
  67. define A::operator<(RBMagnitude) and within this function you will use
  68. the IsKindOf() method (assuming you are using MFC and CObject
  69. derivatives, otherwise, use the new RTTI operators) to verify the
  70. RBMagnitude passed in is an A class, then do the comparisons.  The
  71. other overloaded operators will call this one and will thus accomplish
  72. exactly what you want.
  73.  
  74. One final note, though:  do not make the other "equality" operators
  75. virtual.  The whole purpose is to not have to overload them for the
  76. new class, so there is no reason for them to be virtual.
  77.  
  78. Proper implementation:
  79.  
  80. class RBMagnitude
  81. {
  82.    virtual int operator ==(RBMagnitude& x) const = 0;
  83.    virtual int operator <(RBMagnitude& x) const = 0;
  84.    int operator >(RBMagnitude& x) { return !(*this == x || *this<x);}
  85.    int operator <=(RBMagnitude& x) { return (*this == x || *this<x);}
  86.    int operator >=(RBMagnitude& x) { return !(*this < x); }
  87.    int operator ==(RBMagnitude& x) { return !(*this == x); }
  88. };
  89.  
  90. class A : public RBMagnitude, public CObject
  91. {
  92.    virtual int operator ==(RBMagnitude& x)
  93.       { if (x.IsKindOf(RUNTIME_CLASS(A)) return n == x.n;
  94.          else return 0; }
  95.    virtual int operator <(RBMagnitude& x)
  96.       { if (x.IsKindOf(RUNTIME_CLASS(A)) return n < x.n;
  97.         else return 0; }
  98. };
  99. -----
  100. William E. Kempf          : http://www.novia.net/~srwillrd
  101. "Sir Willard"             : mailto:srwillrd@novia.net (home)
  102. Knight of the Ascii Table : mailto:wekempf@marlton.1dc.com (work)
  103.